﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZP.Common;
using ZP.DTO.Common;
using ZP.IBLL;
using ZPJWeb.Filter;

namespace ZPJWeb.Controllers
{
    [AuthorityFilter]
    public class BaseController : Controller
    {
        public Iep_base_infoService _ep_base_infoServic { get; set; }
        public Iep_basic_dataService _ep_basic_dataServic { get; set; }
        public Iep_product_infoService _ep_product_infoServic { get; set; }
        public Iep_personnel_statsService _ep_personnel_statsServic { get; set; }
        public Iep_ip_infoService _ep_ip_infoServic { get; set; }
        public Iep_cert_qualifiedService _ep_cert_qualifiedServic { get; set; }

        public Iht_honor_titleService _ht_honor_titleService { get; set; }   //= Container.Resolve<Iep_honor_titleService>();
        public Iht_type1_titleService _ht_type1_titleService { get; set; }
        public Iht_type1_fileService _ht_type1_fileService { get; set; }
        public Iep_investorService _ep_investorService { get; set; }
        public Iep_business_productService _ep_business_productService { get; set; }
        public Iep_personnel_infoService _ep_personnel_infoService { get; set; }

        public Iep_gov_supportService _ep_gov_supportService { get; set; }

        protected string SaveAsUploadFile(HttpPostedFileBase oFile, string child_dir)
        {
            string guid = Guid.NewGuid().ToString();
            //string[] info = oFile.FileName.Split('.');
            string filename = guid + System.IO.Path.GetExtension(oFile.FileName); //guid + 文件后缀
            string fileDir = ZPCommon.Upload_Save_DIR + child_dir;    //根据企业ID、荣誉ID、标题ID存放文件
            string filepath = Server.MapPath(fileDir) + @"\" + filename; //物理路径
            if (!System.IO.Directory.Exists(Server.MapPath(fileDir)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(fileDir));
            }
            oFile.SaveAs(filepath);

            string result_filepath = child_dir + "/" + filename; //相对路径
            return result_filepath;
        }

        /// <summary>
        /// 共用下载方法
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="downloadname">下载文件显示名称</param>
        /// <returns></returns>
        [AuthorityFilter]
        protected FileContentResult Download(string filename, string downloadname = "")
        {
            FileContentResult fileresult = null;
            if (string.IsNullOrWhiteSpace(filename))
            {
                return fileresult;
            }
            string strGenarateDir = Server.MapPath(@"~/tempFile/exp");
            string strExportFile = strGenarateDir + "\\" + filename;
            //TODO 判断文件类型
            var leixing = "xlsx";
            if (leixing == "xlsx")
            {
                fileresult = File(System.IO.File.ReadAllBytes(strExportFile), "application/vnd.ms-excel", Url.Encode(downloadname + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"));
            }
            else if (leixing == "jpg")
            {
                //TODO
            }
            System.IO.File.Delete(strExportFile);
            return fileresult;
        }
    }
}